//
//  HWPayWebController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/1.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import WebKit
import ZLPhotoBrowser
class HWPayWebController: HWBaseViewController {
    var webView: WKWebView!
    var urlString = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationView.backButton.setImage(UIImage(named: "fanhui"), for: .normal)
        self.navigationView.titleLabel.text = "收益"
        let config = WKWebViewConfiguration()
        config.allowsInlineMediaPlayback = true
        config.userContentController = WKUserContentController()
        config.userContentController.add(self, name: "gameinfo")
        
        webView = WKWebView.init(frame: CGRect.zero, configuration: config)

        let url = URL(string: urlString)
        let request = URLRequest(url: url!)
        webView.load(request)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        self.view.addSubview(webView)
        
        webView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.navigationView.snp.bottom)
        }
        
    }

}

extension HWPayWebController:WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
    }
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        print(message)
        if message == "web图片上传1" {
            let config = ZLPhotoConfiguration.default()
            config.languageType = .chineseSimplified
            config.maxSelectCount = 1
            config.allowSelectOriginal = false
            config.allowSelectGif = false
            config.allowSelectVideo = false
            config.allowTakePhotoInLibrary = false
            let ac = ZLPhotoPreviewSheet(selectedAssets:[])
            
            ac.selectImageBlock = { [weak self] (images, assets, isOriginal) in

                HWGameCircleHttpManager().uploadImage(uploadImage: images[0], success: { urlString in
                    print(urlString)
//                    self?.model?.icon = urlString
//                    self?.collectionView.reloadData()
//                    choosePhoto.hide()
                    webView.evaluateJavaScript("javascript:upload_img('\(urlString)')", completionHandler: nil)
                }, failure: { error in

                })

            }
            ac.showPhotoLibrary(sender: self)
        }
        completionHandler()
    }
}
